<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="45%" append-to-body :before-close="closeDelDialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="280px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题" prop="title" label-width="120px">
              <el-input v-model="form.title" placeholder="请输入标题" clearable style="width: 90%" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="年度" prop="year" label-width="120px">
              <el-date-picker v-model="form.year" type="year" placeholder="选择年" clearable style="width: 90%"
                :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="" prop="annualFileList">
              <span slot="label">
                <em style="color:red"> * </em>
                年报
              </span>
              <div>
                <div  v-show="showDelete"  style="display: flex; flex-wrap: wrap; /* 允许子元素换行 */">
                  <el-button class="upload_btn" type="primary" icon="el-icon-upload"
                    v-if="fileMJ == undefined && isFbState" @click="uploadTap">上传excel</el-button>
                  <el-upload v-else class="upload-file-demo" action="上传的url" ref="annualUpload" :auto-upload="true"
                    :show-file-list='false' :accept="accept" :http-request="annualFile" :limit="1" :before-upload="beforeUpload"
                    :file-list="annualFileList">
                    <div style="width: 100%; display: flex">
                      <el-button type="primary" icon="el-icon-upload">上传excel</el-button>
                    </div>
                  </el-upload>
                  <el-select v-if="isFbState" style="margin-left:10px;" v-model="fileMJ" placeholder="请选择文件密级">
                    <el-option v-for="(item, idx) in renyuanOptions" :key="idx" :label="item.dictLabel"
                      :value="item.dictValue"></el-option>
                  </el-select>
                </div>

                <div v-for="(item, index) in annualFileList" style="display: flex; margin-left: 1%" :key="index">
                  <span style="padding: 0 5px">{{ item.originalFile }}</span>
                  <p v-if="isFbState" style="border-bottom: 1px solid red;">
                      <span v-for="(i, idx) in renyuanOptions" :key="idx">
                        {{ item.mj == i.dictValue ? i.dictLabel : '' }}
                      </span>
                    </p>
                  <el-button type="text" @click="onPreviewTap(item)" style="margin-left:10px">预览</el-button>
                  <el-button type="text" v-show="showDelete" style="color: #ff0000"
                    @click="deleteTap(item)">删除</el-button>
                </div>
              </div>
            </el-form-item>
          </el-col>

<!--          <el-col :span="24">-->
<!--            <el-form-item label="" prop="storeroomFileList">-->
<!--              <span slot="label">-->
<!--                <em style="color:red"> * </em>-->
<!--                档案库房、设施、设备配置使用情况-->
<!--              </span>-->
<!--              <div >-->
<!--                <div v-show="showDelete" style="display: flex; flex-wrap: wrap; /* 允许子元素换行 */">-->
<!--                  <el-button class="upload_btn" type="primary" icon="el-icon-upload"-->
<!--                    v-if="fileMJ1 == undefined && isFbState" @click="uploadTap1">上传文件</el-button>-->
<!--                    <el-upload v-else class="upload-file-demo" action="上传的url" ref="storeroomUpload" :auto-upload="true"-->
<!--                  :show-file-list='false'  :accept="accept" :http-request="storeroomFile"-->
<!--                  :file-list="storeroomFileList">-->
<!--                  <div style="width: 100%; display: flex">-->
<!--                    <el-button type="primary" icon="el-icon-upload">上传文件</el-button>-->
<!--                  </div>-->
<!--                </el-upload>-->
<!--                <el-select v-if="isFbState" style="margin-left:10px;" v-model="fileMJ1" placeholder="请选择文件密级">-->
<!--                    <el-option v-for="(item, idx) in renyuanOptions" :key="idx" :label="item.dictLabel"-->
<!--                      :value="item.dictValue"></el-option>-->
<!--                  </el-select>-->
<!--                </div>-->
<!--                -->
<!--                <div v-for="(item, index) in storeroomFileList" style="display: flex; margin-left: 1%" :key="index">-->
<!--                  <span style="padding: 0 5px">{{ item.originalFile }}</span>-->
<!--                  <p v-if="isFbState" style="border-bottom: 1px solid red;">-->
<!--                      <span v-for="(i, idx) in renyuanOptions" :key="idx">-->
<!--                        {{ item.mj == i.dictValue ? i.dictLabel : '' }}-->
<!--                      </span>-->
<!--                    </p>-->
<!--                  <el-button type="text" @click="onPreviewTap(item)" style="margin-left:10px">预览</el-button>-->
<!--                  <el-button type="text" v-show="showDelete" style="color: #ff0000"-->
<!--                    @click="deleteTap(item)">删除</el-button>-->
<!--                </div>-->
<!--              </div>-->
<!--            </el-form-item>-->
<!--          </el-col>-->

<!--          <el-col :span="24">-->
<!--            <el-form-item label="" prop="personnelFileList">-->
<!--              <span slot="label">-->
<!--                <em style="color:red"> * </em>-->
<!--                档案工作人员管理情况-->
<!--              </span>-->
<!--              <div>-->
<!--              <div v-show="showDelete" style="display: flex; flex-wrap: wrap; /* 允许子元素换行 */">-->
<!--                <el-button class="upload_btn" type="primary" icon="el-icon-upload"-->
<!--                    v-if="fileMJ2 == undefined && isFbState" @click="uploadTap2">上传文件</el-button>-->
<!--                    <el-upload v-else class="upload-file-demo" action="上传的url" ref="personnelUpload" :auto-upload="true"-->
<!--                  :show-file-list='false'  :accept="accept" :http-request="personnelFile"-->
<!--                  :file-list="personnelFileList">-->
<!--                  <div style="width: 100%; display: flex">-->
<!--                    <el-button type="primary" icon="el-icon-upload" >上传文件</el-button>-->
<!--                  </div>-->
<!--                </el-upload>-->
<!--                <el-select v-if="isFbState" style="margin-left:10px;" v-model="fileMJ2" placeholder="请选择文件密级">-->
<!--                    <el-option v-for="(item, idx) in renyuanOptions" :key="idx" :label="item.dictLabel"-->
<!--                      :value="item.dictValue"></el-option>-->
<!--                  </el-select>-->
<!--              </div>-->
<!--                -->
<!--                <div v-for="(item, index) in personnelFileList" style="display: flex; margin-left: 1%" :key="index">-->
<!--                  <span style="padding: 0 5px">{{ item.originalFile }}</span>-->
<!--                  <p v-if="isFbState" style="border-bottom: 1px solid red;">-->
<!--                      <span v-for="(i, idx) in renyuanOptions" :key="idx">-->
<!--                        {{ item.mj == i.dictValue ? i.dictLabel : '' }}-->
<!--                      </span>-->
<!--                    </p>-->
<!--                  <el-button type="text" @click="onPreviewTap(item)" style="margin-left:10px">预览</el-button>-->
<!--                  <el-button type="text" v-show="showDelete" style="color: #ff0000"-->
<!--                    @click="deleteTap(item)">删除</el-button>-->
<!--                </div>-->
<!--              </div>-->
<!--            </el-form-item>-->
<!--          </el-col>-->

<!--          <el-col :span="24">-->
<!--            <el-form-item label="" prop="collectFileList">-->
<!--              <span slot="label">-->
<!--                <em style="color:red"> * </em>-->
<!--                档案收集、整理、保管、提供利用等情况-->
<!--              </span>-->
<!--              <div>-->
<!--                <div v-show="showDelete" style="display: flex; flex-wrap: wrap; /* 允许子元素换行 */">-->
<!--                  <el-button class="upload_btn" type="primary" icon="el-icon-upload"-->
<!--                    v-if="fileMJ3 == undefined && isFbState" @click="uploadTap3">上传文件</el-button>-->
<!--                  <el-upload v-else class="upload-file-demo" action="上传的url" ref="collectUpload" :auto-upload="true"-->
<!--                  :show-file-list='false'  :accept="accept" :http-request="collectFile"-->
<!--                  :file-list="collectFileList">-->
<!--                  <div style="width: 100%; display: flex">-->
<!--                    <el-button type="primary" icon="el-icon-upload">上传文件</el-button>-->
<!--                  </div>-->
<!--                </el-upload>-->
<!--                <el-select v-if="isFbState" style="margin-left:10px;" v-model="fileMJ3" placeholder="请选择文件密级">-->
<!--                    <el-option v-for="(item, idx) in renyuanOptions" :key="idx" :label="item.dictLabel"-->
<!--                      :value="item.dictValue"></el-option>-->
<!--                  </el-select>-->
<!--                </div>-->
<!--               -->
<!--                <div v-for="(item, index) in collectFileList" style="display: flex; margin-left: 1%" :key="index">-->
<!--                  <span style="padding: 0 5px">{{ item.originalFile }}</span>-->
<!--                  <p v-if="isFbState" style="border-bottom: 1px solid red;">-->
<!--                      <span v-for="(i, idx) in renyuanOptions" :key="idx">-->
<!--                        {{ item.mj == i.dictValue ? i.dictLabel : '' }}-->
<!--                      </span>-->
<!--                    </p>-->
<!--                  <el-button type="text" @click="onPreviewTap(item)" style="margin-left:10px">预览</el-button>-->
<!--                  <el-button type="text" v-show="showDelete" style="color: #ff0000"-->
<!--                    @click="deleteTap(item)">删除</el-button>-->
<!--                </div>-->
<!--              </div>-->
<!--            </el-form-item>-->
<!--          </el-col>-->

<!--          <el-col :span="24">-->
<!--            <el-form-item label="" prop="secureFileList">-->
<!--              <span slot="label">-->
<!--                <em style="color:red"> * </em>-->
<!--                档案信息化建设和信息安全保障情况-->
<!--              </span>-->
<!--              <div>-->
<!--              <div v-show="showDelete" style="display: flex; flex-wrap: wrap; /* 允许子元素换行 */">-->
<!--                <el-button class="upload_btn" type="primary" icon="el-icon-upload"-->
<!--                    v-if="fileMJ4 == undefined && isFbState" @click="uploadTap4">上传文件</el-button>-->
<!--                <el-upload v-else class="upload-file-demo" action="上传的url" ref="secureUpload" :auto-upload="true"-->
<!--                  :show-file-list='false'  :accept="accept" :http-request="secureFile"-->
<!--                  :file-list="secureFileList">-->
<!--                  <div style="width: 100%; display: flex">-->
<!--                    <el-button type="primary" icon="el-icon-upload">上传文件</el-button>-->
<!--                  </div>-->
<!--                </el-upload>-->
<!--                <el-select v-if="isFbState" style="margin-left:10px;" v-model="fileMJ4" placeholder="请选择文件密级">-->
<!--                    <el-option v-for="(item, idx) in renyuanOptions" :key="idx" :label="item.dictLabel"-->
<!--                      :value="item.dictValue"></el-option>-->
<!--                  </el-select>-->
<!--              </div>-->
<!--               -->
<!--                <div v-for="(item, index) in secureFileList" style="display: flex; margin-left: 1%" :key="index">-->
<!--                  <span style="padding: 0 5px">{{ item.originalFile }}</span>-->
<!--                  <p v-if="isFbState" style="border-bottom: 1px solid red;">-->
<!--                      <span v-for="(i, idx) in renyuanOptions" :key="idx">-->
<!--                        {{ item.mj == i.dictValue ? i.dictLabel : '' }}-->
<!--                      </span>-->
<!--                    </p>-->
<!--                  <el-button type="text" @click="onPreviewTap(item)" style="margin-left:10px">预览</el-button>-->
<!--                  <el-button type="text" v-show="showDelete" style="color: #ff0000"-->
<!--                    @click="deleteTap(item)">删除</el-button>-->
<!--                </div>-->
<!--              </div>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-show="form.state != '4'">
        <!--        <el-button @click="saveForm" v-show="showDelete">保 存</el-button>-->
        <el-button type="primary" @click="submitForm" v-show="showDelete">提 交</el-button>
        <el-button type="primary" @click="passThrough" v-show="!showDelete">通 过</el-button>
        <el-button type="danger" @click="returnTab" v-show="!showDelete">退 回</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="45%" append-to-body>
      <div>
        <!--
         文件预览
         1、修改添加文件预览istype="1"
         2、查看组件详情istype="2"
         -->
        <FilePreview v-if="item" :item="item" :istype="istype" :moudle="1"></FilePreview>
      </div>
    </el-dialog>

    <!-- 退回 -->
    <Return :title="returnTitle" :open="returnOpen" @closeDelDialog="close" :showDelete="true" :dataInfo="dataInfo">
    </Return>
  </div>
</template>

<script>
import { deleteFile, uploadFile } from "@/api/archive/supervision/annualInspectionNotice";
import FilePreview from "@/views/archive/supervision/supervises/filePreview";
import Return from "@/views/archive/supervision/materialReporting/return";
import { updateById } from "@/api/archive/supervision/materialReporting";
import { getDicts_archive } from '@/api/archive/dict/dictManageData'
import { fbstat } from "@/api/archive/fileCollection/offlinReception.js";
import {
  getdamjbyrymj
} from "@/api/archive/fileCollection/offlinReception.js";
export default {
  name: "selfTest",
  props: ['title', 'open', 'dataInfo', 'showDelete'],
  components: { FilePreview, Return },
  data() {
    const checkAnnualFileList = (rule, value, callback) => {
      if (this.annualFileList.length === 0) {
        //callback(new Error('请上传excel文件'));
        this.$message.warning('请上传excel文件')
      } else {
        callback();
      }
    };
    // const checkStoreroomFileList = (rule, value, callback) => {
    //   if (this.storeroomFileList.length === 0) {
    //     callback(new Error('请上传文件'));
    //   } else {
    //     callback();
    //   }
    // };
    // const checkPersonnelFileList = (rule, value, callback) => {
    //   if (this.personnelFileList.length === 0) {
    //     callback(new Error('请上传文件'));
    //   } else {
    //     callback();
    //   }
    // };
    // const checkCollectFileList = (rule, value, callback) => {
    //   if (this.collectFileList.length === 0) {
    //     callback(new Error('请上传文件'));
    //   } else {
    //     callback();
    //   }
    // };
    // const checkSecureFileList = (rule, value, callback) => {
    //   if (this.secureFileList.length === 0) {
    //     callback(new Error('请上传文件'));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      form: {
        title: undefined,
        year: undefined,
        noticeContent: undefined
      },
      rules: {
        annualFileList: [
          { validator: checkAnnualFileList, trigger: "blur" }
        ]
        // storeroomFileList: [
        //   { validator: checkStoreroomFileList, trigger: "blur" }
        // ],
        // personnelFileList: [
        //   { validator: checkPersonnelFileList, trigger: "blur" }
        // ],
        // collectFileList: [
        //   { validator: checkCollectFileList, trigger: "blur" }
        // ],
        // secureFileList: [
        //   { validator: checkSecureFileList, trigger: "blur" }
        // ],
      },
      accept: '.xls, .xlsx',
      annualFileList: [],
      storeroomFileList: [],
      personnelFileList: [],
      collectFileList: [],
      secureFileList: [],
      viewOpen: false,
      viewTitle: '',
      item: '',
      istype: '1',
      returnTitle: '',
      returnOpen: false,
      renyuanOptions: [],//密级下拉列表
      isFbState: false,
      fileMJ: undefined,
      fileMJ1: undefined,
      fileMJ2: undefined,
      fileMJ3: undefined,
      fileMJ4: undefined,
    }
  },
  watch: {
    dataInfo: {
      handler(val) {
        console.log(val,"------------------------------")
        this.form = JSON.parse(JSON.stringify(val));
        this.form.attachmentsList = val.attachmentsList.map(item => {
          // 为每个对象添加 docType 属性，并赋值为 originalFile后缀
          item.docType = this.getType(item.originalFile)
          return item;
        });
        this.annualFileList = this.form.attachmentsList.filter(item => item.fileType == '-1')
        // this.storeroomFileList = this.form.attachmentsList.filter(item => item.fileType == '5')
        // this.personnelFileList = this.form.attachmentsList.filter(item => item.fileType == '6')
        // this.collectFileList = this.form.attachmentsList.filter(item => item.fileType == '7')
        // this.secureFileList = this.form.attachmentsList.filter(item => item.fileType == '8')
      },
      deep: true
    }
  },
  created(){
    fbstat().then(res => {  //false=不是分保，true=是分保
      this.isFbState = res.data
      if (this.isFbState) {
        //密级下拉列表
        getDicts_archive("MJ").then(res => { //人员密级
          getdamjbyrymj().then(resdata => { //此接口没有返回dictLabel，需要组装接口
            var mjdata = [];
            for (var i in resdata.data) {
              for (var j in res.data) {
                if (resdata.data[i].daMj == res.data[j].dictValue) {
                  mjdata.push(res.data[j])
                }
              }
            }

            this.renyuanOptions = mjdata
          })
        })
      }
    })
  },
  methods: {
    beforeUpload(file){ // 上传前 文件校验
      //this.files = file;
      const extension = file.name.split('.')[1] === 'xls'
      const extension2 = file.name.split('.')[1] === 'xlsx'

      if (!extension && !extension2) {
        this.$message.warning('上传文件只能是 xls、xlsx格式!')
        return false;
      }
      //return false; // 返回false不会自动上传
    },
    uploadTap() {
      if (this.fileMJ == undefined) {
        this.$message.warning(`请选择密级`);
        return false
      }
    },

    closeDelDialog() {
      this.$emit('closeDelDialog')
    },
    saveForm() {

    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.attachmentsList = this.form.attachmentsList.filter(item => item.fileType != '4' && item.fileType != '5' && item.fileType != '6' && item.fileType != '7' && item.fileType != '8')
          if (this.annualFileList.length != 0) {
            this.form.attachmentsList.push(...this.annualFileList)
          }

          // 设置状态为2
          this.form.state = '2'
          updateById(this.form).then(res => {
            this.$modal.msgSuccess('提交成功')
            this.closeDelDialog()
          }).catch(() => {
            this.closeDelDialog()
          })
        }
      })
    },
    passThrough() {
      // 设置状态为4
      this.form.state = '4'
      updateById(this.form).then(res => {
        this.$modal.msgSuccess('提交成功')
        this.closeDelDialog()
      }).catch(() => {
        this.closeDelDialog()
      })
    },
    returnTab() {
      this.returnTitle = '填写退回原因'
      this.returnOpen = true
    },
    close() {
      this.returnOpen = false
      this.closeDelDialog()
    },
    // 获取文件类型
    getType(fileName) {
      let arr = fileName.split('.');
      let result = arr.pop();
      return result;
    },
    // 年度档案工作责任制和管理制度落实情况
    annualFile(content) {
      this.file = content.file
      const formData = new FormData();
      formData.append("file", content.file);
      formData.append("type", '-1');
      formData.append("id", this.form.id);
      if(this.fileMJ){
        formData.append("mj", this.fileMJ);
      }

      uploadFile(formData).then(response => {
        this.annualFileList.push({
          filePath: response.data.filePath,
          id: response.data.id,
          fileName: response.data.fileName,
          fileType: response.data.fileType,
          originalFile: response.data.originalFile,
          docType: this.getType(response.data.originalFile),
          mj: response.data.mj
        })
      })
    },
    // 档案库房、设施、设备配置使用情况
    storeroomFile(content) {
      this.file = content.file
      const formData = new FormData();
      formData.append("file", content.file);
      formData.append("type", '5');
      formData.append("id", this.form.id);
      if(this.fileMJ1){
        formData.append("mj", this.fileMJ1);
      }

      uploadFile(formData).then(response => {
        this.storeroomFileList.push({
          filePath: response.data.filePath,
          id: response.data.id,
          fileName: response.data.fileName,
          fileType: response.data.fileType,
          originalFile: response.data.originalFile,
          docType: this.getType(response.data.originalFile),
          mj: response.data.mj
        })
      })
    },
    // 档案工作人员管理情况
    personnelFile(content) {
      this.file = content.file
      const formData = new FormData();
      formData.append("file", content.file);
      formData.append("type", '6');
      formData.append("id", this.form.id);
      if(this.fileMJ2){
        formData.append("mj", this.fileMJ2);
      }
      uploadFile(formData).then(response => {
        this.personnelFileList.push({
          filePath: response.data.filePath,
          id: response.data.id,
          fileName: response.data.fileName,
          fileType: response.data.fileType,
          originalFile: response.data.originalFile,
          docType: this.getType(response.data.originalFile),
          mj: response.data.mj
        })
      })
    },
    // 档案收集、整理、保管、提供利用等情况
    collectFile(content) {
      this.file = content.file
      const formData = new FormData();
      formData.append("file", content.file);
      formData.append("type", '7');
      formData.append("id", this.form.id);
      if(this.fileMJ3){
        formData.append("mj", this.fileMJ3);
      }
      uploadFile(formData).then(response => {
        this.collectFileList.push({
          filePath: response.data.filePath,
          id: response.data.id,
          fileName: response.data.fileName,
          fileType: response.data.fileType,
          originalFile: response.data.originalFile,
          docType: this.getType(response.data.originalFile),
          mj: response.data.mj
        })
      })
    },
    // 档案信息化建设和信息安全保障情况
    secureFile(content) {
      this.file = content.file
      const formData = new FormData();
      formData.append("file", content.file);
      formData.append("type", '8');
      formData.append("id", this.form.id);
      if(this.fileMJ4){
        formData.append("mj", this.fileMJ4);
      }
      uploadFile(formData).then(response => {
        this.secureFileList.push({
          filePath: response.data.filePath,
          id: response.data.id,
          fileName: response.data.fileName,
          fileType: response.data.fileType,
          originalFile: response.data.originalFile,
          docType: this.getType(response.data.originalFile),
          mj: response.data.mj
        })
      })
    },
    // 预览
    onPreviewTap(item) {
      if (item.docType == 'OFD' || item.docType == 'ofd') {
        this.$modal.msgWarning('OFD文件不支持预览')
      } else {
        this.viewTitle = item.originalFile
        this.viewOpen = true
        this.item = item
      }
    },
    // 删除
    deleteTap(item) {
      var form = {
        noticesId: this.dataInfo.id,
        attachmentsId: item.id,
        attachmentsType: item.fileType
      }
      this.$modal.confirm('是否确认删除名为"' + item.originalFile + '"的附件？').then(function () {
        return deleteFile(form)
      }).then(res => {
        this.getFileList(form)
        this.$modal.msgSuccess('删除成功')
      }).catch(() => { });
    },
    getFileList(form) {
      if (form.attachmentsType == '4') {
        this.annualFileList = this.annualFileList.filter(item => item.id !== form.attachmentsId);
      }
      // if (form.attachmentsType == '5') {
      //   this.storeroomFileList = this.storeroomFileList.filter(item => item.id !== form.attachmentsId);
      // }
      // if (form.attachmentsType == '6') {
      //   this.personnelFileList = this.personnelFileList.filter(item => item.id !== form.attachmentsId);
      // }
      // if (form.attachmentsType == '7') {
      //   this.collectFileList = this.collectFileList.filter(item => item.id !== form.attachmentsId);
      // }
      // if (form.attachmentsType == '8') {
      //   this.secureFileList = this.secureFileList.filter(item => item.id !== form.attachmentsId);
      // }

    }
  }
}
</script>

<style scoped></style>
